// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   ChartLabel.java

package com.java4less.rchart;

import com.java4less.rchart.gc.ChartGraphics;
import com.java4less.rchart.gc.ChartImage;
import com.java4less.rchart.gc.GraphicsProvider;
import com.java4less.rchart.gc.Polygon;
import com.java4less.rchart.tags.TagObject;
import com.java4less.rchart.tags.TagParser;
import java.util.Vector;

// Referenced classes of package com.java4less.rchart:
//			IFloatingObject, Chart, ChartLoader, LineStyle, 
//			FillStyle, Axis, Scale

public class ChartLabel
	implements IFloatingObject
{

	protected static int ALIGN_LEFT = 0;
	protected static int ALIGN_CENTER = 1;
	protected static int ALIGN_RIGHT = 2;
	protected static int BORDER_RECT = 0;
	protected static int BORDER_ROUNDRECT = 1;
	protected static int BORDER_OVAL = 2;
	protected Polygon clickableArea;
	protected Chart chart;
	private String sFormat;
	int lineWidths[];
	int lineHeights[];
	int lineCount;
	int requiredWidth;
	int requiredHeight;
	TagObject objects[];
	protected FillStyle background;
	protected LineStyle border;
	protected int borderShape;
	protected String clickInfo;
	protected int rotation;
	protected int rotationAlign;
	protected int positionX;
	protected int positionY;
	protected int align;
	protected int anchorX;
	protected int anchorY;
	protected int marginX;
	protected int marginY;
	protected LineStyle lineToAnchor;
	protected String name;
	protected String tip;
	protected boolean ignorePosition;

	public ChartLabel(String pformat, String pvalue, boolean pvertical, boolean pCenter)
	{
		clickableArea = null;
		chart = null;
		sFormat = "";
		lineWidths = new int[100];
		lineHeights = new int[100];
		lineCount = 0;
		requiredWidth = 0;
		requiredHeight = 0;
		objects = null;
		background = null;
		border = null;
		borderShape = BORDER_RECT;
		clickInfo = "";
		rotation = 0;
		rotationAlign = ChartGraphics.ROTATE_CENTER;
		positionX = 0;
		positionY = 0;
		align = ALIGN_LEFT;
		anchorX = 0;
		anchorY = 0;
		marginX = 2;
		marginY = 2;
		lineToAnchor = null;
		name = "";
		tip = "";
		ignorePosition = false;
		if (pformat.length() == 0)
			pformat = pvalue;
		if (pvertical)
			pformat = (new StringBuilder()).append("@rotation value='90'@").append(pformat).toString();
		if (pCenter)
			pformat = (new StringBuilder()).append("@align value='CENTER'@").append(pformat).toString();
		int p = pformat.indexOf("#value#");
		if (p >= 0)
			pformat = (new StringBuilder()).append(pformat.substring(0, p - 1)).append(pvalue).append(pformat.substring(p + 7)).toString();
		sFormat = pformat;
	}

	protected int getRotatedWidth()
	{
		if (rotation == 90 || rotation == -90 || rotation == 270)
			return requiredHeight;
		else
			return requiredWidth;
	}

	protected int getRotatedHeight()
	{
		if (rotation == 90 || rotation == -90 || rotation == 270)
			return requiredWidth;
		else
			return requiredHeight;
	}

	public void initialize(ChartGraphics g, Chart c)
	{
		chart = c;
		TagParser tagParser = new TagParser();
		objects = tagParser.parseTags(sFormat);
		preProcessTags(g);
	}

	public void paint(ChartGraphics g, int x, int y, int width, int height)
	{
		preProcessPositionTags(g, x, y, width, height);
		if (chart != null)
			chart.placeFloatingObject(this);
		if (chart != null)
			chart.addFloationgObject(this);
		render(g);
	}

	protected void preProcessTags(ChartGraphics g)
	{
		com.java4less.rchart.gc.ChartFont tmpFont = g.getFont();
		positionX = 0;
		positionY = 0;
		requiredWidth = 1;
		requiredHeight = 1;
		lineHeights[lineCount] = g.getFontHeight();
		for (int i = 0; i < objects.length; i++)
		{
			if (objects[i].compareAtt(TagObject.ATT_TYPE, TagObject.TAG_LF))
			{
				lineCount++;
				lineHeights[lineCount] = g.getFontHeight();
			}
			if (objects[i].compareAtt(TagObject.ATT_TYPE, TagObject.TAG_STRING))
				lineWidths[lineCount] = lineWidths[lineCount] + g.getFontWidth(objects[i].getAttribute(TagObject.ATT_VALUE));
			if (!objects[i].compareAtt(TagObject.ATT_TYPE, TagObject.TAG_OBJECT))
				continue;
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_ALIGN))
			{
				if (objects[i].compareAtt(TagObject.ATT_VALUE, "CENTER"))
					align = ALIGN_CENTER;
				if (objects[i].compareAtt(TagObject.ATT_VALUE, "LEFT"))
					align = ALIGN_LEFT;
				if (objects[i].compareAtt(TagObject.ATT_VALUE, "RIGHT"))
					align = ALIGN_RIGHT;
			}
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_BACKGROUND))
				background = ChartLoader.convertFillStyle(objects[i].getAttribute(TagObject.ATT_VALUE));
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_BORDER))
			{
				border = ChartLoader.convertLineStyle(objects[i].getAttribute(TagObject.ATT_VALUE));
				if (objects[i].compareAtt("SHAPE", "ROUNDRECT"))
					borderShape = BORDER_ROUNDRECT;
				if (objects[i].compareAtt("SHAPE", "OVAL"))
					borderShape = BORDER_OVAL;
			}
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_ROTATION))
			{
				rotation = parseInt(objects[i].getAttribute(TagObject.ATT_VALUE));
				if (rotation != 90 && rotation != -90 && rotation != 180 && rotation != 270 && rotation != 45 && rotation != -45)
					rotation = 0;
				if (objects[i].compareAtt("CENTER", "LEFTTOP"))
					rotationAlign = ChartGraphics.ROTATE_LEFTTOP;
			}
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_CLICKINFO))
				clickInfo = objects[i].getAttribute(TagObject.ATT_VALUE);
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_TIP))
				tip = objects[i].getAttribute(TagObject.ATT_VALUE);
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_POSITION) && objects[i].compareAtt("IGNORE", "TRUE"))
				ignorePosition = true;
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_SIZE) && !ignorePosition)
			{
				String items[] = ChartLoader.convertList(objects[i].getAttribute(TagObject.ATT_VALUE), ",");
				if (items == null)
					items = new String[0];
				if (items.length > 0)
				{
					int tmp = calcX(items[0]);
					if (tmp > requiredWidth)
						requiredWidth = tmp;
				}
				if (items.length > 1)
				{
					int tmp = calcY(items[1]);
					if (tmp > requiredHeight)
						requiredHeight = tmp;
				}
			}
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_MARGIN))
			{
				String items[] = ChartLoader.convertList(objects[i].getAttribute(TagObject.ATT_VALUE), ",");
				if (items == null)
					items = new String[0];
				if (items.length > 0)
					marginX = calcX(items[0]);
				if (items.length > 1)
					marginY = calcY(items[1]);
			}
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_FONT))
			{
				com.java4less.rchart.gc.ChartFont font = ChartLoader.convertFont(objects[i].getAttribute(TagObject.ATT_VALUE));
				if (font != null)
				{
					g.setFont(font);
					if (g.getFontHeight() > lineHeights[lineCount])
						lineHeights[lineCount] = g.getFontHeight();
				}
			}
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_IMAGE))
			{
				ChartImage image = GraphicsProvider.getImage(objects[i].getAttribute(TagObject.ATT_VALUE));
				lineWidths[lineCount] = lineWidths[lineCount] + image.getWidth();
			}
		}

		int w = 0;
		int h = marginY * 2;
		for (int i = 0; i <= lineCount; i++)
		{
			int lineW = lineWidths[i] + marginX * 2;
			if (w < lineW)
				w = lineW;
			h += lineHeights[i];
		}

		if (w > requiredWidth)
			requiredWidth = w;
		if (h > requiredHeight)
			requiredHeight = h;
		g.setFont(tmpFont);
	}

	protected void preProcessPositionTags(ChartGraphics g, int x, int y, int width, int height)
	{
		positionX = x;
		positionY = y;
		requiredWidth = width;
		requiredHeight = height;
		for (int i = 0; i < objects.length; i++)
		{
			if (!objects[i].compareAtt(TagObject.ATT_TYPE, TagObject.TAG_OBJECT))
				continue;
			String items[];
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_POSITION))
			{
				if (objects[i].compareAtt("IGNORE", "TRUE"))
					ignorePosition = true;
				if (!ignorePosition)
				{
					items = ChartLoader.convertList(objects[i].getAttribute(TagObject.ATT_VALUE), ",");
					if (items == null)
						items = new String[0];
					if (items.length > 0)
						positionX = calcRelativeX(items[0], x);
					if (items.length > 1)
						positionY = calcRelativeY(items[1], y);
				}
			}
			int tmp;
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_ANCHOR) && !ignorePosition)
			{
				lineToAnchor = ChartLoader.convertLineStyle(objects[i].getAttribute("LINE"));
				items = ChartLoader.convertList(objects[i].getAttribute(TagObject.ATT_VALUE), ",");
				if (items == null)
					items = new String[0];
				if (items.length > 0)
				{
					tmp = calcX(items[0]);
					anchorX = tmp;
				}
				if (items.length > 1)
				{
					tmp = calcY(items[1]);
					anchorY = tmp;
				}
			}
			if (!objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_SIZE) || ignorePosition)
				continue;
			items = ChartLoader.convertList(objects[i].getAttribute(TagObject.ATT_VALUE), ",");
			if (items == null)
				items = new String[0];
			if (items.length > 0)
			{
				tmp = calcX(items[0]);
				if (tmp > requiredWidth)
					requiredWidth = tmp;
			}
			if (items.length <= 1)
				continue;
			tmp = calcY(items[1]);
			if (tmp > requiredHeight)
				requiredHeight = tmp;
		}

		int w = 0;
		int h = marginY * 2;
		for (int i = 0; i <= lineCount; i++)
		{
			int lineW = lineWidths[i] + marginX * 2;
			if (w < lineW)
				w = lineW;
			h += lineHeights[i];
		}

		if (w > requiredWidth)
			requiredWidth = w;
		if (h > requiredHeight)
			requiredHeight = h;
	}

	protected void render(ChartGraphics g2)
	{
		ChartGraphics g = g2;
		ChartImage rotatedImage = null;
		int tmpPositionX = positionX;
		int tmpPositionY = positionY;
		if (lineToAnchor != null && rotation == 0)
			if (anchorY > positionY)
			{
				if (anchorX <= positionX)
					lineToAnchor.draw(g2, anchorX, anchorY, positionX, (positionY + requiredHeight) - 1);
				else
					lineToAnchor.draw(g2, anchorX, anchorY, positionX + requiredWidth, (positionY + requiredHeight) - 1);
			} else
			if (anchorX <= positionX)
				lineToAnchor.draw(g2, anchorX, anchorY, positionX, positionY);
			else
				lineToAnchor.draw(g2, anchorX, anchorY, positionX + requiredWidth, positionY);
		if (rotation != 0)
		{
			com.java4less.rchart.gc.ChartColor transparentColor = GraphicsProvider.getColor(1, 1, 1);
			rotatedImage = GraphicsProvider.createTransparentImage(requiredWidth, requiredHeight, transparentColor);
			g = rotatedImage.getGraphics();
			g.setFont(g2.getFont());
			g.setColor(g2.getColor());
			positionX = 0;
			positionY = 0;
		}
		int line = 0;
		int leftMargin = 0;
		int x = positionX;
		int y = positionY + marginY;
		int lineStart = 0;
		if (background != null)
		{
			com.java4less.rchart.gc.ChartColor c = g.getColor();
			if (borderShape == BORDER_RECT)
				background.draw(g, positionX, positionY, (positionX + requiredWidth) - 1, (positionY + requiredHeight) - 1);
			if (borderShape == BORDER_ROUNDRECT)
				background.drawRoundRect(g, positionX, positionY, (positionX + requiredWidth) - 1, (positionY + requiredHeight) - 1);
			if (borderShape == BORDER_OVAL)
				background.drawArc(g, (int)((double)positionX - (double)requiredWidth * 0.10000000000000001D), (int)((double)positionY - (double)requiredHeight * 0.10000000000000001D), (int)(((double)requiredWidth + (double)requiredWidth * 0.20000000000000001D) - 1.0D), (int)(((double)requiredHeight + (double)requiredHeight * 0.29999999999999999D) - 1.0D), 0, 360);
			g.setColor(c);
		}
		if (align == ALIGN_CENTER)
			x += (requiredWidth - marginX * 2 - lineWidths[0]) / 2;
		if (align == ALIGN_RIGHT)
			x += requiredWidth - marginX * 2 - lineWidths[0];
		lineStart = x + marginX;
		x = lineStart;
		for (int i = 0; i < objects.length; i++)
		{
			if (objects[i].compareAtt(TagObject.ATT_TYPE, TagObject.TAG_LF))
			{
				line++;
				leftMargin = 0;
				x = positionX;
				y += lineHeights[line - 1];
				if (align == ALIGN_CENTER)
					x += (requiredWidth - marginX * 2 - lineWidths[line]) / 2;
				if (align == ALIGN_RIGHT)
					x += requiredWidth - marginX * 2 - lineWidths[line];
				lineStart = x + marginX;
			}
			if (objects[i].compareAtt(TagObject.ATT_TYPE, TagObject.TAG_BACKSPACE))
				x -= g.getFontWidth(" ");
			if (objects[i].compareAtt(TagObject.ATT_TYPE, TagObject.TAG_CR))
				x = lineStart;
			if (objects[i].compareAtt(TagObject.ATT_TYPE, TagObject.TAG_STRING))
			{
				String s = objects[i].getAttribute(TagObject.ATT_VALUE);
				g.drawString(s, x, y + lineHeights[line]);
				x += g.getFontWidth(s);
			}
			if (!objects[i].compareAtt(TagObject.ATT_TYPE, TagObject.TAG_OBJECT))
				continue;
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_FONT))
			{
				com.java4less.rchart.gc.ChartFont font = ChartLoader.convertFont(objects[i].getAttribute(TagObject.ATT_VALUE));
				if (font != null)
					g.setFont(font);
			}
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_COLOR))
			{
				com.java4less.rchart.gc.ChartColor c = ChartLoader.convertColor(objects[i].getAttribute(TagObject.ATT_VALUE));
				if (c != null)
					g.setColor(c);
			}
			if (objects[i].compareAtt(TagObject.ATT_OBJECT_NAME, TagObject.OBJ_IMAGE))
			{
				ChartImage image = GraphicsProvider.getImage(objects[i].getAttribute(TagObject.ATT_VALUE));
				g.drawImage(image, x, y);
				x += image.getWidth();
			}
		}

		if (border != null)
		{
			if (borderShape == BORDER_RECT)
				border.drawRect(g, positionX, positionY, (positionX + requiredWidth) - 1, (positionY + requiredHeight) - 1);
			if (borderShape == BORDER_ROUNDRECT)
				border.drawRoundRect(g, positionX, positionY, (positionX + requiredWidth) - 1, (positionY + requiredHeight) - 1);
			if (borderShape == BORDER_OVAL)
				border.drawArc(g, (int)((double)positionX - (double)requiredWidth * 0.10000000000000001D), (int)((double)positionY - (double)requiredHeight * 0.10000000000000001D), (int)(((double)requiredWidth + (double)requiredWidth * 0.20000000000000001D) - 1.0D), (int)(((double)requiredHeight + (double)requiredHeight * 0.29999999999999999D) - 1.0D), 0, 360);
		}
		clickableArea = null;
		if (rotation != 0)
		{
			positionX = tmpPositionX;
			positionY = tmpPositionY;
			g2.paintRotatedImage(rotatedImage, rotation, positionX, positionY, rotationAlign);
		} else
		{
			clickableArea = new Polygon();
			clickableArea.addPoint(positionX, positionY);
			clickableArea.addPoint(positionX, (positionY + requiredHeight) - 1);
			clickableArea.addPoint((positionX + requiredWidth) - 1, (positionY + requiredHeight) - 1);
			clickableArea.addPoint((positionX + requiredWidth) - 1, positionY);
			if (chart != null)
				chart.chartHotAreas.addElement(this);
		}
	}

	protected int calcRelativeX(String s, int x)
	{
		if (s.length() == 0)
			return x;
		if (s.indexOf("+") == 0)
			return x + calcX(s.substring(1));
		if (s.indexOf("-") == 0)
			return x - calcX(s.substring(1));
		else
			return calcX(s);
	}

	protected int calcRelativeY(String s, int y)
	{
		if (s.length() == 0)
			return y;
		if (s.indexOf("+") == 0)
			return y + calcY(s.substring(1));
		if (s.indexOf("-") == 0)
			return y - calcY(s.substring(1));
		else
			return calcY(s);
	}

	protected int calcX(String s)
	{
		if (s.indexOf("px") > 0)
			return parseInt(s.substring(0, s.indexOf("px")));
		if (s.indexOf("%") > 0)
			return (chart.getWidth() * Integer.parseInt(s.substring(0, s.indexOf("%")))) / 100;
		if (chart.XAxis == null)
			return 0;
		else
			return chart.XAxis.scale.getScreenCoord((new Double(s)).doubleValue());
	}

	protected int calcY(String s)
	{
		if (s.indexOf("px") > 0)
			return parseInt(s.substring(0, s.indexOf("px")));
		if (s.indexOf("%") > 0)
			return (chart.getHeight() * Integer.parseInt(s.substring(0, s.indexOf("%")))) / 100;
		if (chart.YAxis == null)
			return 0;
		else
			return chart.YAxis.scale.getScreenCoord((new Double(s)).doubleValue());
	}

	private int parseInt(String s)
	{
		return Integer.parseInt(s);
		Exception e;
		e;
		e.printStackTrace();
		return 0;
	}

	public String getName()
	{
		return name;
	}

	public String getClickInfo()
	{
		return clickInfo;
	}

	public String getTip()
	{
		return tip;
	}

	public int getX()
	{
		return positionX;
	}

	public void setX(int x)
	{
		positionX = x;
	}

	public int getY()
	{
		return positionY;
	}

	public void setY(int y)
	{
		positionY = y;
	}

	public Polygon getObjectBounds()
	{
		Polygon pol = new Polygon();
		pol.addPoint(positionX, positionY);
		pol.addPoint(positionX + requiredWidth, positionY);
		pol.addPoint(positionX + requiredWidth, positionY + requiredHeight);
		pol.addPoint(positionX, positionY + requiredHeight);
		return pol;
	}

}
